using System;
using DevExtreme.AspNet.Data;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;

namespace Leo.Core.DevExtreme
{
    public class DevExtremeDataLoadOptionsBinderProvider : IModelBinderProvider
    {
        public IModelBinder GetBinder(ModelBinderProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context.Metadata.ContainerType != null
                && context.Metadata.ContainerType == typeof(DataSourceLoadOptionsBase)
                && context.Metadata.ModelType == typeof(System.Collections.IList))
            {
                return new BinderTypeModelBinder(typeof(DevExtremeFilterModelBinder));
            }

            return null;
        }
    }
}
